//
//  FirstTranslasion.swift
//  elarc01
//
//  Created by elarc on 16/4/15.
//  Copyright © 2016年 elarc. All rights reserved.
//

import Foundation


class FirstTranslasionVC:UIViewController,UIViewControllerTransitioningDelegate  {
    
    private var percentDrivenTransition: UIPercentDrivenInteractiveTransition!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.whiteColor()
        self.transitioningDelegate = self
        
        //添加手势，用以Push
        addScreenEdgePanGestureRecognizer(self.view, edges: UIRectEdge.Right)
        
        let btn = UIButton(type: .Custom)
        btn.frame = CGRectMake(100, 100, 100, 40)
        btn.titleLabel?.text = "pushTo"
        btn.backgroundColor = UIColor.yellowColor()
        self.view.addSubview(btn)
        btn.addTarget(self, action: #selector(FirstTranslasionVC.pushTo), forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    func pushTo () {
        let secondVC = SecondTranslasionVC ()
        secondVC.transitioningDelegate = self
        addScreenEdgePanGestureRecognizer(secondVC.view, edges: UIRectEdge.Left)
        self.navigationController?.pushViewController(secondVC, animated: true)
    }
    
    func edgePanGesture(edgePan: UIScreenEdgePanGestureRecognizer) {
        let progress = abs(edgePan.translationInView(UIApplication.sharedApplication().keyWindow!).x) / UIApplication.sharedApplication().keyWindow!.bounds.width
        
        if edgePan.state == UIGestureRecognizerState.Began {
            self.percentDrivenTransition = UIPercentDrivenInteractiveTransition()
            if edgePan.edges == UIRectEdge.Right {
                self.pushTo()
            } else if edgePan.edges == UIRectEdge.Left {
                self.dismissViewControllerAnimated(true, completion: nil)
            }
        } else if edgePan.state == UIGestureRecognizerState.Changed {
            self.percentDrivenTransition?.updateInteractiveTransition(progress)
        } else if edgePan.state == UIGestureRecognizerState.Cancelled || edgePan.state == UIGestureRecognizerState.Ended {
            if progress > 0.5 {
                self.percentDrivenTransition?.finishInteractiveTransition()
            } else {
                self.percentDrivenTransition?.cancelInteractiveTransition()
            }
            self.percentDrivenTransition = nil
        }
    }
    
    
    //动画Push
    
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return ElarcFlipTransionPush()
    }
    //动画Pop
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return ElarcFlipTransionPop()
    }
    
    //百分比Push
    func interactionControllerForPresentation(animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
        return self.percentDrivenTransition
    }
    //百分比Pop
    func interactionControllerForDismissal(animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
        return self.percentDrivenTransition
    }
    
    func addScreenEdgePanGestureRecognizer(view: UIView, edges: UIRectEdge) {
        let edgePan = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(FirstTranslasionVC.edgePanGesture(_:)))
        edgePan.edges = edges
        view.addGestureRecognizer(edgePan)
    }
}